﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace ZyGame
{
    public static class TextureExtension
    {
        public static void SaveTexture2D(this Texture2D texture, string path)
        {
#if UNITY_EDITOR
            File.Delete(path);
            File.WriteAllBytes(path, texture.EncodeToPNG());
            UnityEditor.AssetDatabase.SaveAssets();
            UnityEditor.AssetDatabase.Refresh();
            UnityEditor.TextureImporter textureImporter = (UnityEditor.TextureImporter)UnityEditor.AssetImporter.GetAtPath(path);
            UnityEditor.TextureImporterPlatformSettings textureImporterPlatformSettings = textureImporter.GetDefaultPlatformTextureSettings();
            textureImporter.isReadable = true;
            textureImporter.mipmapEnabled = false;
            textureImporter.alphaIsTransparency = true;
            textureImporterPlatformSettings.maxTextureSize = 512;
            textureImporterPlatformSettings.compressionQuality = 100;
            textureImporterPlatformSettings.format = UnityEditor.TextureImporterFormat.Automatic;
            textureImporterPlatformSettings.resizeAlgorithm = UnityEditor.TextureResizeAlgorithm.Mitchell;
            UnityEditor.AssetDatabase.SaveAssets();
            UnityEditor.AssetDatabase.Refresh();
#endif
        }

    }
}
